<template>
    <el-dialog :visible="dialogVisible" width="680px" title="新增规则" @close="closeDialog" :close-on-click-modal="false">
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-position="top">
          <el-form-item label="计费规则编号" prop="ruleNumber">
            <el-input v-model="addForm.ruleNumber" />
          </el-form-item>

          <el-form-item label="计费规则名称" prop="ruleName">
            <el-input v-model="addForm.ruleName" />
          </el-form-item>

          <el-form-item label="计费方式" prop="chargeType">
            <el-radio-group v-model="addForm.chargeType" size="small">
              <el-radio label="duration" border>时长收费</el-radio>

              <el-radio label="turn" border>按次收费</el-radio>

              <el-radio label="partition" border>分段收费</el-radio>

            </el-radio-group>

          </el-form-item>

          <div class="flex-container">
            <el-form-item label="免费时长">
              <el-input v-model="addForm.freeDuration" />
            </el-form-item>

            <el-form-item label="收费上限">
              <el-input v-model="addForm.chargeCeiling" />
            </el-form-item>

          </div>

          <el-form-item label="计费规则" :prop="feeRuleProp">
            <!-- 按时长收费区域 -->
            <!-- el-form-item可以继续嵌套el-form-item -->
            <div  class="duration"  v-if="addForm.chargeType === 'duration'">
              每 
              <el-form-item prop="durationTime">
                <el-input v-model="addForm.durationTime" class="input-box" />
              </el-form-item> 
              小时 
              <el-form-item prop="durationPrice">
                <el-input v-model="addForm.durationPrice" class="input-box" />
              </el-form-item>
               元
            </div>

            <!-- 按次收费区域 -->
            <div class="turn" v-if="addForm.chargeType === 'turn'">
              每次 
              <el-form-item prop="turnPrice">
                <el-input v-model="addForm.turnPrice" class="input-box" />
              </el-form-item>
               元
            </div>

            <!-- 按分段收费区域 -->
            <div class="partition" v-if="addForm.chargeType === 'partition'">
              <div class="item">
                <el-form-item prop="partitionFrameTime">
                  <el-input v-model="addForm.partitionFrameTime" class="input-box" />
                </el-form-item>
                小时内,每小时收费
                <el-form-item prop="partitionFramePrice">
                  <el-input v-model="addForm.partitionFramePrice" class="input-box" />
                </el-form-item>
                 元</div>

              <div class="item">每增加
                <el-form-item prop="partitionIncreaseTime">
                  <el-input v-model="addForm.partitionIncreaseTime" class="input-box" />
                </el-form-item>
                小时,增加
                <el-form-item prop="partitionIncreasePrice">
                  <el-input v-model="addForm.partitionIncreasePrice" class="input-box" />
                </el-form-item>
                 元</div>

            </div>

          </el-form-item>

        </el-form>

      </div>

      <template #footer>
        <el-button size="mini" @click="closeDialog">取 消</el-button>

        <el-button size="mini" type="primary" @click="confirmAdd">确 定</el-button>

      </template>

    </el-dialog>
</template>

<script>
import { addFeeRuleAPI, editFeeRuleAPI } from '@/api/rule'
export default {
    props: {
        dialogVisible: {
            type: Boolean,
            default: false
        },
        //新增prop接收编辑数据
        eidtData: {
          type: Object,
          default: () => null
        }
    },
  data() {
    return {
      feeRuleProp: 'durationTime',
      addForm: {
        id: null,
        ruleNumber: '', // 计费规则编号
        ruleName: '', // 计费规则名称
        chargeType: 'duration', // 计费规则类型 duration / turn / partition
        chargeCeiling: null,
        freeDuration: null,
        // 时长计费独有字段
        durationTime: null, // 时长计费单位时间
        durationPrice: null, // 时长计费单位价格
        durationType: 'hour',
        // 按次收费独有字段
        turnPrice: null,
        // 分段计费独有字段
        partitionFrameTime: null, // 段内时间
        partitionFramePrice: null, // 段内费用
        partitionIncreaseTime: null, // 超出时间
        partitionIncreasePrice: null // 超出费为收费价钱
      },
      addFormRules: {
        ruleNumber: [{ required: true, message: '请输入规则编号', trigger: 'blur' }],
        ruleName: [{ required: true, message: '请输入规则名称', trigger: 'blur' }],
        chargeType: [{ required: true, message: '请选择收费类型', trigger: 'blur' }],
        durationTime: [{ required: true, message: '请输入时间', trigger: 'blur' }],
        durationPrice: [{ required: true, message: '请输入金额', trigger: 'blur' }],
        turnPrice: [{ required: true, message: '请输入次数', trigger: 'blur' }],
        partitionFrameTime: [{ required: true, message: '请输入段内时间', trigger: 'blur' }],
        partitionFramePrice: [{ required: true, message: '请输入段内费用', trigger: 'blur' }],
        partitionIncreaseTime: [{ required: true, message: '请输入超出时间', trigger: 'blur' }],
        partitionIncreasePrice: [{ required: true, message: '请输入超出金额', trigger: 'blur' }],
      },
    }
  },
  //监听文本框数据变化
  watch: {
    'addForm.chargeType'(val) {
      //文本框发生切换，清空所有的校验提示
      this.$refs.addForm.clearValidate(['durationTime', 'durationPrice', 'turnPrice', 'partitionFrameTime', 'partitionFramePrice', 'partitionIncreaseTime', 'partitionIncreasePrice',])
      if (val === 'duration') {
        this.feeRuleProp = 'durationTime'
      }
      if (val === 'turn') {
        this.feeRuleProp = 'turnPrice'
      }
      if (val === 'partition') {
        this.feeRuleProp = 'partitionFrameTime'
      }
    },
    //监听editData编辑数据变化
    editData: {
      handler(newVal) {
        if (newVal) {
          // 深拷贝避免污染源数据
          const formData = JSON.parse(JSON.stringify(newVal))
          
          // 合并到表单对象（保留默认值，覆盖存在的值）
          this.addForm = {
            ...this.addForm, // 保留初始默认值
            ...formData       // 覆盖编辑数据
          }
          
          // 特殊字段处理（比如数值类型转换）
          this.addForm.freeDuration = Number(this.addForm.freeDuration)
          this.addForm.chargeCeiling = Number(this.addForm.chargeCeiling)
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    //点击确认按钮实现提交表单功能
    async confirmAdd() {
      this.$refs.addForm.validate(async flag => {
        if (!flag) return

        try {
          //根据id判断是新增还是编辑
          const apiMethod = this.addForm.id ? editFeeRuleAPI() : addFeeRuleAPI()
          await apiMethod(this.addForm)
          this.$message.success(this.addForm.id ? '更新成功' : '添加成功')
          this.$emit('getList')
          this.closeDialog()
        } catch (error) {
           
        } 
        // console.log('可以提交接口了')
        /* await addFeeRuleAPI(this.addForm)
        this.$message.success('添加成功') */
        //this.getRuleList()
        //子组件调用父组件的方法
        /* 1.this.$parent适用简单场景不复用场景
        2.this.$emit适用简单场景可以复用*/
        //this.$parent.getRuleList()
        
      })
    },
    //关闭弹框
    closeDialog() {
      this.$emit('update:dialogVisible', false)
      //this.dialogVisible = false
      this.$refs.addForm.resetFields()
      this.addForm.freeDuration = ''
      this.addForm.chargeCeiling = ''
    },
  }
}
</script>

<style lang="scss" scoped>
.form-container{
  padding:0px 80px;
}
</style>